﻿using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;

namespace TK.Mqtt.Client
{
    /// <summary>
    /// mqtt client host service
    /// </summary>
    public class MqttHostedService : BackgroundService
    {
        private readonly IMultiMqttClientWrapper _multiMqttClientWrapper;

        /// <summary>
        /// ctor
        /// </summary>
        public MqttHostedService(IMultiMqttClientWrapper multiMqttClientWrapper)
        {
            _multiMqttClientWrapper = multiMqttClientWrapper;
        }

        /// <summary>
        /// ExecuteAsync
        /// </summary>
        protected async override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            if (!stoppingToken.IsCancellationRequested)
            {
                await _multiMqttClientWrapper.StartClientAsync();
            }
        }
    }
}
